package main

import (
	"context"
	"fmt"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

const mongoApplyURL = "mongodb://root:123456@localhost:47017/?authSource=car_rental&readPreference=primary&ssl=false"
const database = "car_rental"

type T []interface{}

func main() {

	c := context.Background()
	mc, err := mongo.Connect(c, options.Client().ApplyURI(mongoApplyURL))

	if err != nil {
		panic(err)
	}

	col := mc.Database(database).Collection("account")

	findRows(c, col)
}

func findRows(c context.Context, col *mongo.Collection) {

	cur, err := col.Find(c, bson.M{})

	if err != nil {
		panic(err)
	}

	for cur.Next(c) {

		var row struct {
			ID     primitive.ObjectID `bson:"_id"`
			OpenID string             `bson:"open_id"`
		}

		err := cur.Decode(&row)

		if err != nil {
			panic(err)
		}

		fmt.Printf("%+v\n", row)

	}

}

func insertMany(c context.Context, col *mongo.Collection) {

	res, err := col.InsertMany(c, T{
		bson.M{
			"open_id": "abc",
		},
		bson.M{
			"open_id": "666",
		},
	})

	if err != nil {
		panic(err)
	}

	fmt.Printf("%+v", res)

}
